<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('cate_class',LIB_PATH);

$main = new Cate($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
$options = $main->get_cate_option();
//dump($options);
switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller,$cf_action,0) ;
		$data_array = $main->get_tree();
		//dump($data_array);
	break;
	case "add":
		if($cf_ispost){
			$data = $db->getFormData($main->get_table_name(),$_POST);

			$result = $main->insert_data($data);

			if($result){
				//更新菜单静态文件
				$main->update_data_file();
                $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                unset($_SESSION['pre_url']);
				$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
				my_jump('添加成功'.$more,'',$return_page,3);
			}
			
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$cate_parent_id = intval($_GET['pid']);
			$row['cate_pid'] =  $cate_parent_id ? $cate_parent_id : '';

		}
	break;
	case "edit":
		if($cf_ispost){

			$data = $db->getFormData($main->get_table_name(),$_POST);

			$result = $main->update_data($data,"cate_id='$id'");
			
			if($result){
                            $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                            unset($_SESSION['pre_url']);
				//更新菜单静态文件
				$main->update_data_file();
				my_jump('修改成功','',$return_page,1);
			}else{
                            my_jump('修改失败');
            }
				
		}else{
			$smarty->mypage($cf_controller,$cf_action,0) ;
			$id = intval($_GET['id']);
			$row = $main->get_data_row($id);
			if(!$row)
			my_jump('沒有此分類信息');

            $_SESSION['pre_url']  = $cf_pre_page ? $cf_pre_page  : ''; 
		}
	break;
	case "del":
		$id = intval($_GET['id']);
		//$db->debug(1,1);
		$result = $main->del_data("cate_id='$id'");
		
		if($result){
			//更新菜单静态文件
			$main->update_data_file();
			my_jump('删除成功','',$return_page,1);
		}else{
			my_jump('删除失败!');
		}
		
	break;
	case "set":
		$type = $_GET['type'];
		$value = $_GET['value'];
		$where = "cate_id = '".$id."'";
		$result = $main->set_data($value,$type,$where);
		if($result){
			//更新菜单静态文件
			$main->update_data_file();
			my_jump('更新完成','',$return_page,1);
		}else{ 
			my_jump('更新失败');
		}
	break;
	default:
		my_jump('非法操作!');
	break;
	
	
	
}

$smarty->assign(array(
	'id'=>$id,
	'row'=>htmlchar($row),
	'data_array'=>$data_array,
	 'options' => add_array_item(array('0' => '根目录'), $options),

));
$smarty->mydisplay();
?>